import { storage } from "@/utils/storage"
import router from "."
import { checkTokenIsExpired } from "@/api/common.api"
// import { useAuthStore } from "@/store/auth"

// const authStore = useAuthStore()

router.beforeEach(async (to, from, next) => {
  if (to.path !== "/login") {
    const checkToken = await checkTokenIsExpired()
    if (checkToken.data.code !== 11000) {
      // token过期
      // authStore.setToken("")
      // 跳转登录
      next({ path: "/login", query: { redirect: from.path } })
      return
    }

    const token = storage.get("token")
    if (token) {
      next()
    } else {
      next({ path: "/login" })
    }
  } else {
    // authStore.setToken("")
    next()
  }
})
